<template>
  <el-tree
    ref="tree"
    node-key="id"
    show-checkbox
    default-expand-all
    check-strictly
    :data="list"
    :props="{label:'name'}"
  />
</template>
<script>
import { getPermissionList } from '@/api/permisson'
import { transDataToTreeData } from '@/utils/'
import { getRoleDetail, assignPerm } from '@/api/setting'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async hSubmit() {
      const permIds = this.$refs.tree.getCheckedKeys()
      await assignPerm({ id: this.curId, permIds: permIds })
      this.$emit('success')
    },
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log('getPermissionList', res)
        const detail = await getRoleDetail(this.curId)
        console.log(detail)
        // 进行数据回显，设置选择
        this.$refs.tree.setCheckedKeys(detail.data.permIds)
        // 保存数据
        if (res.code === 10000) {
          // 转换成树形数据
          this.list = transDataToTreeData(res.data)
        }
      } catch (err) {
        console.log('getPermissionList', err)
      }
    }
  }
}
</script>
